<HTML>
<HEAD>
<TITLE>[Chapter 13] 13.2 IllegalComponentStateException</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:52:29 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 13<br>AWT Exceptions and Errors</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch13_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-13-SECT-2">13.2 IllegalComponentStateException</A></h2>

<P CLASS=para>
<tt CLASS=literal>IllegalComponentStateException</tt> 
is a subclass of <tt CLASS=literal>IllegalStateException</tt>; 
both are new to Java 1.1. This exception is used when you try to do something 
with a <tt CLASS=literal>Component</tt> that is not 
yet appropriate. With the standard AWT components, this can happen only 
in three instances:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If you call <tt CLASS=literal>setCaretPosition()</tt> 
to set the cursor position of a text component before the component's 
peer exists.

<P>
<li CLASS=listitem>If you call <tt CLASS=literal>getLocale()</tt> 
to get the locale of a component that does not have one and is not in 
a container that has one.

<P>
<li CLASS=listitem>If you call <tt CLASS=literal>getLocationOnScreen()</tt> 
for a component that is not showing.

<P>
</UL>
<P CLASS=para>
In these cases, the operation isn't 
fundamentally illegal; you are just trying to perform it before the component 
is ready. When you create your own components, you should consider using 
this exception for similar cases. 

<P CLASS=para>
Since <tt CLASS=literal>IllegalComponentStateException</tt> 
is a subclass of <tt CLASS=literal>Run-TimeException</tt>, 
you do not have to enclose method calls that might throw this exception 
within <tt CLASS=literal>try/catch</tt> blocks. However, catching this exception isn't a bad idea, since it should be fairly easy to correct the problem and retry the operation.

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-13-SECT-2.1">IllegalComponentStateException Method</A></h3>Constructor

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public IllegalComponentStateException () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The first constructor creates an <tt CLASS=literal>IllegalComponentStateException</tt> 
instance with no detail message. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public IllegalComponentStateException (String message)  <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
This constructor creates an <tt CLASS=literal>IllegalComponentStateException</tt> 
with a detail message of <tt CLASS=literal>message</tt>. 
This message can be retrieved using <tt CLASS=literal>getMessage()</tt>, 
which it inherits from <tt CLASS=literal>Exception</tt> 
(and is required by the <tt CLASS=literal>Throwable</tt> 
interface). </DL>
</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-13-SECT-2.2">IllegalComponentStateException Example</A></h3>

<P CLASS=para>
The following code throws an <tt CLASS=literal>IllegalComponentStateException</tt>. 
The <tt CLASS=literal>Exception</tt> occurs because 
the <tt CLASS=literal>TextField</tt> peer does not 
exist when <tt CLASS=literal>setCaretPosition()</tt> 
is called. <tt CLASS=literal>setCaretPosition()</tt> 
throws an <tt CLASS=literal>IllegalComponentStateException</tt>, 
and the next statement never executes. 

<DIV CLASS=screen>
<P>
<PRE>
import java.awt.TextField;
public class illegal {
    public static void main (String[] args) {
        new TextField().setCaretPosition (24);
        System.out.println ("Never gets here");
    }
}
</PRE>
</DIV>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch13_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch13_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>AWTException</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>AWTError</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
